﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Net.Http;
using HtmlAgilityPack;

namespace HappyChinese
{
    class Chinese
    {
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr dwReserved);
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

        private string sno;
        private string time = "0";
        private bool running = true;
        private string name = "Logging";
        private HttpClient client;
        public delegate void MsgChangedDelegate(object sender, string msg);
        public event MsgChangedDelegate MsgChanged;

        public Chinese(string sno)
        {
            client = new HttpClient(new HttpClientHandler() { UseCookies = true });
            this.sno = sno;
        }

        public void Start()
        {
            Login();
        }

        private void Login()
        {
            Dictionary<string, string> form = new Dictionary<string, string>();
            form.Add("ctl00$ContentPlaceHolder1$name", sno);
            form.Add("ctl00$ContentPlaceHolder1$pwd", sno);
            form.Add("ctl00$ContentPlaceHolder1$login", "登陆");
            form.Add("__VIEWSTATE", "");
            client.PostAsync("http://59.69.102.9/zgyw/index.aspx", new FormUrlEncodedContent(form)).ContinueWith(t =>
            {
                var response = t.Result;
                response.Content.ReadAsStringAsync().ContinueWith(o =>
                {
                    var msg = o.Result;
                    HtmlDocument html = new HtmlDocument();
                    html.LoadHtml(msg);
                    var timebox = html.GetElementbyId("ctl00_ContentPlaceHolder1_lblonlineTime");
                    time = timebox.InnerText.Replace("&nbsp;", "");
                    var namebox = html.GetElementbyId("ctl00_ContentPlaceHolder1_lblrealname");
                    name = namebox.InnerText;
                });
            }).Wait();
            Study();
        } 

        private void Study()
        {
            while (running)
            {
                client.PostAsync("http://59.69.102.9/zgyw/study/LearningIndex.aspx", new FormUrlEncodedContent(new Dictionary<string,string>())).Wait();
                Thread.Sleep(200);
                client.PostAsync("http://59.69.102.9/zgyw/index.aspx", new FormUrlEncodedContent(new Dictionary<string, string>())).ContinueWith(t =>
                {
                    var response = t.Result;
                    response.Content.ReadAsStringAsync().ContinueWith(o =>
                    {
                        var msg = o.Result;
                        HtmlDocument html = new HtmlDocument();
                        html.LoadHtml(msg);
                        var timebox = html.GetElementbyId("ctl00_ContentPlaceHolder1_lblonlineTime");
                        time = timebox.InnerText.Replace("&nbsp;", "");
                        var namebox = html.GetElementbyId("ctl00_ContentPlaceHolder1_lblrealname");
                        name = namebox.InnerText;
                        if (time.Contains('分'))
                        {
                            if(Convert.ToInt32(time.Split('分')[0]) > 1010)
                            {
                                Stop();
                            }
                        }
                    });
                }).Wait();
                MsgChanged?.Invoke(this, ToString());
                GC.Collect();
                Thread.Sleep(800);
            }
        }

        public void Stop()
        {
            running = false;
        }

        public override string ToString()
        {
            return string.Format("用户:{0}\t时长:{1}", name, time);
        }
    }
}
